package bb.lanxing.lib.devices.sprint.sync;

import android.content.Context;
import android.util.Log;
import com.google.gson.Gson;
import bb.lanxing.lib.devices.core.sync.DeviceFile;
import bb.lanxing.lib.devices.core.sync.DeviceFileStatus;
import bb.lanxing.lib.devices.sprint.entity.SprintMaps;
import bb.lanxing.lib.devices.sprint.model.SprintMapModel;
import java.util.List;
import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;

public class SprintMapSyncManager extends SprintMapManager implements SprintMapModel.Callback {
    private DeviceFile currentDeviceFile;

    public SprintMapSyncManager(Context context, String str) {
        super(context, str);
    }

    @Override
    protected Observable<List<SprintMaps>> loadSprintMapsFromModels() {
        return Observable.create(new Observable.OnSubscribe<List<SprintMaps>>() {
            @Override
            public void call(Subscriber<? super List<SprintMaps>> subscriber) {
                List<SprintMaps> list;
                List<SprintMaps> list2 = null;
                try {
                    list = SprintMapSyncManager.this.serverMapModel.loadSprintMaps();
                } catch (Exception e) {
                    Log.d("loadSprintMapsFrom", "serverSprintMaps" + e.getMessage());
                    list = null;
                }
                try {
                    list2 = SprintMapSyncManager.this.staticMapModel.loadSprintMaps();
                } catch (Exception e2) {
                    Log.d("loadSprintMapsFrom", "localSprintMaps" + e2.getMessage());
                }
                Gson gson = new Gson();
                Log.d("loadSprintMapsFrom", "serverSprintMaps: " + gson.toJson(list));
                Log.d("loadSprintMapsFrom", "localSprintMaps: " + gson.toJson(list2));
                if (list == null || list.isEmpty()) {
                    list = list2;
                }
                try {
                    SprintMapSyncManager.this.sprintMapModel.loadSprintMaps();
                    if (subscriber.isUnsubscribed()) {
                        return;
                    }
                    subscriber.onNext(list);
                    subscriber.onCompleted();
                } catch (Exception e3) {
                    subscriber.onError(e3);
                }
            }
        }).subscribeOn(Schedulers.io());
    }

    @Override
    public boolean isSynchronising() {
        return this.currentDeviceFile != null;
    }

    @Override
    protected boolean onSync(DeviceFile deviceFile) {
        if (!this.controller.isConnected()) {
            notifySyncStatus(deviceFile, DeviceFileStatus.STATUS_SYNC_FAIL.getStatus());
            return false;
        }
        long id = deviceFile.getId();
        if (this.serverMapModel.isDownloaded(id)) {
            if (isSynchronised(id)) {
                return false;
            }
            this.sprintMapModel.syncWithDevice(this.serverMapModel.getSprintMapById(deviceFile.getId()));
        } else {
            this.serverMapModel.downloadMapById(id);
        }
        this.currentDeviceFile = deviceFile;
        return true;
    }

    @Override
    public int getSyncState(long j) {
        int syncState = super.getSyncState(j);
        if (!isSynchronised(j) && DeviceFileStatus.STATUS_SYNCED.getStatus() == syncState) {
            syncState = DeviceFileStatus.STATUS_NONE.getStatus();
            DeviceFile deviceFileById = getDeviceFileById(j);
            if (deviceFileById != null) {
                notifySyncStatus(deviceFileById, syncState);
            }
        }
        return syncState;
    }

    @Override
    public void onMapStatus(long j, int i) {
        DeviceFileStatus vStatus = DeviceFileStatus.valueOf(i);
        if (vStatus == DeviceFileStatus.STATUS_SYNCED
                || vStatus == DeviceFileStatus.STATUS_DOWNLOAD_FAIL
                || vStatus == DeviceFileStatus.STATUS_SYNC_FAIL) {
            this.currentDeviceFile = pop();
        } else if (vStatus == DeviceFileStatus.STATUS_DOWNLOADED) {
            this.sprintMapModel.syncWithDevice(this.serverMapModel.getSprintMapById(j));
            return;
        }
        super.onMapStatus(j, i);
    }

    @Override
    public void release() {
        super.release();
        this.controller = null;
    }
}
